package com.zx.dsptuneup_TriMic.Util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import com.example.base.tools.tools.StringUtils;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;
import com.zx.dsptuneup_TriMic.Adapter.hintspinnerapdater.HintSpinnerAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.widget.ui.ClickableSpinner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.netty.util.internal.StringUtil;

/**
 * 多选批量控制弹框
 */
public class MultiControlDialogUtil {
    public enum MultiControlType {
        /**
         * 远程
         */
        CONTROL_REMOTE,
        /**
         * 音量
         */
        CONTROL_VOLUME,
        /**
         * 静音
         */
        CONTROL_MUTE,
        /**
         * 恢复出厂
         */
        CONTROL_RESET,
        /**
         * 重启
         */
        CONTROL_RESTART,
    }
    private static int[] inputDevelopChannelArr = {Constants.channelEnum.line1,Constants.channelEnum.line2,
            Constants.channelEnum.line3,Constants.channelEnum.line4,Constants.channelEnum.line5,
            Constants.channelEnum.line6,Constants.channelEnum.line7,Constants.channelEnum.line8,
            Constants.channelEnum.line9,Constants.channelEnum.line10,Constants.channelEnum.line11,
            Constants.channelEnum.line12,Constants.channelEnum.line13,Constants.channelEnum.line14}; // 对应开发者通道数据

    private static int[] inputUserChannelArr = {Constants.channelEnum.line1,Constants.channelEnum.line3,
            Constants.channelEnum.line5,Constants.channelEnum.line7, Constants.channelEnum.line8,
            Constants.channelEnum.line9,Constants.channelEnum.line13};

    private static int[] outputDevelopChannelArr = {Constants.channelEnum.line1,Constants.channelEnum.line2,
            Constants.channelEnum.line3,Constants.channelEnum.line4,Constants.channelEnum.line5,
            Constants.channelEnum.line6,Constants.channelEnum.line7,Constants.channelEnum.line8}; // 对应开发者通道数据
    private static int[] outputUserChannelArr = {Constants.channelEnum.line3};

    //  UI
    private static Dialog confirmDialog;
    private static Drawable drawable_selected = null;
    private static Drawable drawable_normal = null;
    private static int currentSelMode = -1;     // 记录当前选择的模式
    private static int currentSelChannel = -1;  // 记录当前选择的通道
    private static int currentSelLine = -1;     // 记录当前选择的线路

    // 当前选择的通道/输入、输出,用于传值参数
    private static String currentSelElementType = null;
    private static int currentSelChannelType = -1;

    private static String[] modeArr   = new String[] {"开发者模式","普通模式"};
    private static String[] channelArr = new String[] {"输入","输出"};
    private static String[][][] lineArr = new String[][][]
            {
                    {
                            //开发者模式 输入
                            {"吊麦1","阵列麦1","3代扩声麦","阵列麦2","3代远程麦","阵列麦3","模拟桌麦","线路1","会议通道","啸叫返回","网络桌麦","保留1","保留2","保留3"},
                            //开发者模式 输出
                            {"AEC参考", "会议输出", "功放输出", "啸叫抑制","通道1","通道2", "通道3", "通道4"}
                    },
                    {
                            //普通模式 输入
                            {"吊麦1","吊麦2","吊麦3","桌麦1","线路","会议通道","桌麦2"},
                            //普通模式 输出
                            {"功放输出"}
                    }
            };


    public static void showAlertDialog(Activity context, boolean canceledOnTouchOutside, final ConfirmDialogListener confirmDialogListener) {
        if (context == null || context.isFinishing()) {
            return;
        }

        initDialog();

        confirmDialog = new Dialog(context, R.style.addDevice_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_multicontroll, null);
        confirmDialog.setContentView(view);
        confirmDialog.setCancelable(canceledOnTouchOutside);

        ClickableSpinner modeSpinner = view.findViewById(R.id.modeSpinner);
        ClickableSpinner channelSpinner = view.findViewById(R.id.channelSpinner);
        ClickableSpinner lineSpinner = view.findViewById(R.id.lineSpinner);

        // 整理数据
        List<String> modeStringList = Arrays.asList(modeArr);
        List<String> channelStringList = Arrays.asList(channelArr);
        List<String> lineStringList    = new ArrayList<>();

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, modeStringList);
        modeSpinner.setAdapter(new HintSpinnerAdapter(adapter1, R.layout.hint_row_item_mode, context));
        adapter1.notifyDataSetChanged();

        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, channelStringList);
        channelSpinner.setAdapter(new HintSpinnerAdapter(adapter2, R.layout.hint_row_item_channel, context));

        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, lineStringList);
        lineSpinner.setAdapter(new HintSpinnerAdapter(adapter3, R.layout.hint_row_item_line, context));


        //  点击控制
        modeSpinner.setOnClickListener(new ClickableSpinner.OnClickListener() {
            @Override
            public void onClick() {
                modeSpinner.performClick();
            }
        });
        channelSpinner.setOnClickListener(new ClickableSpinner.OnClickListener() {
            @Override
            public void onClick() {
                if(modeSpinner.getSelectedItem() == null || StringUtils.isEmpty(modeSpinner.getSelectedItem().toString()) ){
                    ToastUtil.makeText(context,"请先选择模式!");
                    return;
                }else {
                    channelSpinner.performClick();
                }
            }
        });
        lineSpinner.setOnClickListener(new ClickableSpinner.OnClickListener() {
            @Override
            public void onClick() {
                if(modeSpinner.getSelectedItem() == null || StringUtils.isEmpty(modeSpinner.getSelectedItem().toString()) ){
                    ToastUtil.makeText(context,"请先选择模式!");
                    return;
                }else if(channelSpinner.getSelectedItem() == null || StringUtils.isEmpty(channelSpinner.getSelectedItem().toString()) ){
                    ToastUtil.makeText(context,"请先选择通道!");
                    return;
                }
                else {
                    lineSpinner.performClick();
                }
            }
        });

        /**
         * item点击事件
         * */
        modeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i==0){
                    return;
                }
                Log.e("1111","modeSpinner=="+i);
                currentSelMode = i-1;
                if(currentSelChannel == -1 || currentSelMode==-1){
                    return;
                }
                if(currentSelChannel != -1){
                    String[] lineStr = lineArr[currentSelMode][currentSelChannel];

                    List<String> lineStringList = Arrays.asList(lineStr);
                    ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, lineStringList);
                    lineSpinner.setAdapter(new HintSpinnerAdapter(adapter3, R.layout.hint_row_item_line, context));
                    adapter3.notifyDataSetChanged();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        channelSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i==0){
                    return;
                }
                Log.e("1111","channelSpinner=="+i);
                currentSelChannel = i-1;
                if(currentSelChannel == -1 || currentSelMode==-1){
                    return;
                }
                String[] lineStr = lineArr[currentSelMode][currentSelChannel];

                List<String> lineStringList = Arrays.asList(lineStr);
                ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, lineStringList);
                lineSpinner.setAdapter(new HintSpinnerAdapter(adapter3, R.layout.hint_row_item_line, context));
                adapter3.notifyDataSetChanged();

                currentSelElementType = i==1 ? Constants.element_typeEnum.INPUT : Constants.element_typeEnum.OUTPUT;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        lineSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i==0){
                    return;
                }
                Log.e("1111","lineSpinner=="+i);
                currentSelLine = i-1;

                if(currentSelElementType.equals(Constants.element_typeEnum.INPUT)){
                    if(currentSelMode == 0){ // 开发者
                        currentSelChannelType = inputDevelopChannelArr[currentSelLine];
                    }else {
                        currentSelChannelType = inputUserChannelArr[currentSelLine];
                    }
                }else {
                    if(currentSelMode == 0){ // 开发者
                        currentSelChannelType = outputDevelopChannelArr[currentSelLine];
                    }else {
                        currentSelChannelType = outputUserChannelArr[currentSelLine];
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        TextView muteTxt = view.findViewById(R.id.muteTxt);                          // 静音按钮
        Switch remote_switch = view.findViewById(R.id.remote_switch);                // 远程按钮
        IndicatorSeekBar volume_seekbar = view.findViewById(R.id.volume_seekbar);    // 音量调节


        Rect bonds = new Rect(0, 0, 35,35);
        drawable_selected = context.getResources().getDrawable(R.drawable.add_check_sel);
        drawable_selected.setBounds(bonds);

        drawable_normal = context.getResources().getDrawable(R.drawable.add_check_nor);
        drawable_normal.setBounds(bonds);

        //  静音按钮点击逻辑
        muteTxt.setSelected(false);
        muteTxt.setCompoundDrawables(drawable_normal, null, null, null);
        muteTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!checkNextStepValiable(lineSpinner)){
                    ToastUtil.makeText(context,"请先选择线路!");
                    return;
                }
                muteTxt.setSelected(!muteTxt.isSelected());
                if(muteTxt.isSelected()){
                    muteTxt.setText("静音开");
                    muteTxt.setCompoundDrawables(drawable_selected, null, null, null);
                }else{
                    muteTxt.setText("静音关");
                    muteTxt.setCompoundDrawables(drawable_normal, null, null, null);
                }
                if (confirmDialogListener != null && !checkIsNull()) {
                    confirmDialogListener.onClickButton(currentSelElementType,currentSelChannelType,MultiControlType.CONTROL_MUTE,false,muteTxt.isSelected(),0,false,false);
                }
            }
        });

        remote_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if(!checkNextStepValiable(lineSpinner)){
//                    ToastUtil.makeText(context,"请先选择线路!");
//                    remote_switch.setChecked(!isChecked);
//                    return;
//                }
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(currentSelElementType,currentSelChannelType,MultiControlType.CONTROL_REMOTE,isChecked,false,0,false,false);
                }
            }
        });

        volume_seekbar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {
            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
                if(!checkNextStepValiable(lineSpinner)){
                    ToastUtil.makeText(context,"请先选择线路!");
                    seekBar.setProgress(0);
                    return;
                }

                if (confirmDialogListener != null && !checkIsNull()) {
                    confirmDialogListener.onClickButton(currentSelElementType,currentSelChannelType,MultiControlType.CONTROL_VOLUME,false,false,seekBar.getProgressFloat(),false,false);
                }
            }
        });

        // 关闭按钮
        Button close = (Button) view.findViewById(R.id.closeBtn);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });
        Button updata_btn = (Button) view.findViewById(R.id.updata_btn);
        updata_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(currentSelElementType,currentSelChannelType,MultiControlType.CONTROL_RESET,false,false,0,true,false);
                }
            }
        });

        Button restart_btn = (Button) view.findViewById(R.id.restart_btn);
        restart_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(currentSelElementType,currentSelChannelType,MultiControlType.CONTROL_RESTART,false,false,0,false,true);
                }
            }
        });

//        confirmDialog = builder.create();
        confirmDialog.show();

        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = confirmDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.76); //设置宽度
        lp.height = (int) (display.getHeight() * 0.6);

        confirmDialog.getWindow().setAttributes(lp);

        TextView title_tv = (TextView) view.findViewById(R.id.title_tv);
        title_tv.setText("批量控制");

    }
    /**
     *回调参数是否为空
     */
    private static boolean checkIsNull() {
        if(StringUtils.isEmpty(currentSelElementType) || currentSelChannelType == -1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 校验下一步操作是否可执行
     * */
    private static boolean checkNextStepValiable(ClickableSpinner lineSpinner){
        if(lineSpinner.getSelectedItem() == null || StringUtils.isEmpty(lineSpinner.getSelectedItem().toString()) ){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 新的對話框彈出之前，先把舊的清除掉
     */
    private static void initDialog() {
        if (confirmDialog != null && confirmDialog.isShowing()) {
            confirmDialog.dismiss();
            confirmDialog = null;
        }
    }

    public interface ConfirmDialogListener {
        void onClickButton(String elememtType,int channel,MultiControlType multiControlType,boolean isRemote,boolean isMute,float volume,boolean clickReset,boolean clickRestart);
    }
}
